<!DOCTYPE html>
<html>
<head>

</head>
    
<body ontouchstart>
 <script src="js/jquery.min.js"></script>
 <script src="js/weixin/jweixin-1.2.0.js"></script>
 <script>
    $(function() {
         	var unionid=localStorage.getItem('unionid');
        	var token=localStorage.getItem('token');
        	if(token){
        	  window.top.location="index.html";
        	  return;
        	}
            var code=GetQueryString("code");
	        	var crtName=GetQueryString("crtName");

	        	if(crtName){
	        		 localStorage.setItem('crtName',crtName);
	        	}
        	if(unionid){
        		login(unionid) ;
        	}else{

	        	if(code){
	        		getUnionid(code);
	        	}else{
	        		location.href="https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxe82e8e016edc2f0f&redirect_uri="
	        		   +"http://drp.junenng-zh.com/weixin/weix.html&response_type=code&scope=snsapi_base&state=yunnying#wechat_redirect";
	        	}
        	
        	}

        });
        
       
			
		     


function getUnionid(code){
   $.post('/mina/login',{ code: code },function (ret) {
   	if(ret.rel){
         localStorage.setItem('unionid',ret.result.openid);
    	 login(ret.result.openid) ;
    }

  });
}

function GetQueryString(name){
    var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if(r!=null)return  unescape(r[2]); return null;
}

function login(unionid) {
	var formData={"unionid":unionid};
	localStorage.setItem('unionid',unionid);
	 $.post('/mina/xcxLogin',formData,function (ret) {
	 	if(ret.rel){
            localStorage.setItem('token',ret.result.token);
	 		window.top.location="index.html";
	 	}else{
	 	    localStorage.setItem('name',ret.result.nickname);
	 	    localStorage.setItem('headimgurl',ret.result.headimgurl);
	 	    //alert(ret.result.nickname);
	 	 	   window.top.location="register.html";
	 	}
	 });
}
    </script>
</body>
</html>